#include "comm/znx_atomic.h"

void test_atomic_int64()
{
    znx_atomic_int64_t a = 0;
    znx_atomic_int64_reset(&a);

    assert(znx_atomic_int64_load(&a) == 0);

    assert(!znx_atomic_int64_cas(&a, 1, 2));

    assert(znx_atomic_int64_fetch_add(&a, 1) == 0);

    assert(znx_atomic_int64_add_fetch(&a, 1) == 2);

    assert(znx_atomic_int64_cas(&a, 2, 10));
    
    assert(znx_atomic_int64_load(&a) == 10);
}

void test_atomic_bool()
{
    znx_atomic_bool_t b = 0;
    znx_atomic_bool_reset(&b);

    assert(!znx_atomic_bool_is_true(&b));

    znx_atomic_bool_set_true(&b);
    assert(znx_atomic_bool_is_true(&b));

    znx_atomic_bool_set_false(&b);
    assert(!znx_atomic_bool_is_true(&b));
}

void test_atomic_ptr()
{
    znx_atomic_ptr_t p = 0;
    znx_atomic_ptr_reset(&p);

    assert(znx_atomic_ptr_load(&p) == NULL);

    assert(znx_atomic_ptr_swap(&p, (void *)1) == NULL);

    assert(znx_atomic_ptr_cas(&p, (void *)1, (void *)2));

    assert(!znx_atomic_ptr_cas(&p, (void *)1, (void *)3));

    assert(znx_atomic_ptr_load(&p) == (void *)2);
}

int main()
{
    test_atomic_int64();
    test_atomic_bool();
    test_atomic_ptr();

    return 0;
}